package com.avic.rong.goodsSort.adapter.secondListAdapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.product.DetailProductActivity;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllBzBean;
import com.avic.rong.utils.StringUtils;
import com.avic.rong.wedgit.StringUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Chenjiaxin on 2019/7/20 12:55
 * E-Mail Address：16601203965@163.com
 */
public class GoodsSortBzShowRvAdapter extends RecyclerView.Adapter<GoodsSortBzShowRvAdapter.GoodsSortBzShowRvViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<GoodsSortSearchAllBzBean.DataBean> beanList = new ArrayList<>();
    private GoodsBzRvListener goodsBzRvListener;

    public GoodsBzRvListener getGoodsBzRvListener() {
        return goodsBzRvListener;
    }

    public void setGoodsBzRvListener(GoodsBzRvListener goodsBzRvListener) {
        this.goodsBzRvListener = goodsBzRvListener;
    }

    public GoodsSortBzShowRvAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void setBeanList(List<GoodsSortSearchAllBzBean.DataBean> beanList) {
        this.beanList = beanList;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public GoodsSortBzShowRvViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.search_result_peixun_item, viewGroup, false);
        return new GoodsSortBzShowRvViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GoodsSortBzShowRvViewHolder holder, final int i) {

        String title = beanList.get(i).getTitle();
        String title1 = title.substring(beanList.get(i).getNo().length());
        if (!StringUtil.isEmpty(title)) {
            holder.title.setText(title);
        } else {
            holder.title.setText(beanList.get(i).getTitle());
        }

        //设置标题筛选高亮
//        if (beanList != null){
//            SpannableString spannableString = new SpannableString(beanList.get(i).getNo());
//            int startIndex = beanList.get(i).getNo().toLowerCase().indexOf(SearchHomePageActivity.nowSearchStr.toLowerCase());
//            if (startIndex>=0){
//                ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_f23030));
//                spannableString.setSpan(colorSpan, startIndex, startIndex+SearchHomePageActivity.nowSearchStr.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
//                holder.no.setText(spannableString);
//            }else {
//                holder.no.setText(beanList.get(i).getNo());
//            }
//        }
        DecimalFormat df = new DecimalFormat("###0.00");
        String format = df.format(beanList.get(i).getPrice());

        String time = beanList.get(i).getPostTime();
        String newTiem = time.substring(0, time.indexOf("T"));
        if (StringUtils.isEmpty( beanList.get(i).getAttrShow())) {
            holder.type.setVisibility(View.INVISIBLE);
        }else{
            holder.type.setText(beanList.get(i).getAttrShow());
        }

        holder.no.setText(beanList.get(i).getNo());
        holder.fabudata.setText("发布日期:  " + newTiem);
        holder.shishidata.setText("实施日期:  " + newTiem);
        holder.price.setText("¥" + format);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = beanList.get(i).getUrl();
                String[] split = s.split("/");
                Log.e("cjx", "URL：" + split[2]);
                context.startActivity(DetailProductActivity.getIntent(context, split[2]));
            }
        });
        //添加购物车
        holder.car.setOnClickListener(new MyClickListener(holder,beanList.get(i)));
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }


    class MyClickListener implements View.OnClickListener {
        GoodsSortBzShowRvViewHolder holder;
        GoodsSortSearchAllBzBean.DataBean bean;

        public MyClickListener(GoodsSortBzShowRvViewHolder holder, GoodsSortSearchAllBzBean.DataBean bean) {
            this.holder = holder;
            this.bean = bean;
        }

        @Override
        public void onClick(View v) {
            String s = bean.getUrl();
            String[] split = s.split("/");
            Log.e("cjx", "splite:" + split[2]);
            String skuStr = "{" + "\"" + split[2].toString() + "\"" + ":" + "1}";
            goodsBzRvListener.addBzCar(skuStr,holder);
        }
    }


   public static class GoodsSortBzShowRvViewHolder extends RecyclerView.ViewHolder {
        TextView type, no, price, title, fabudata, shishidata;
       public ImageView car;
        LinearLayout item;

        public GoodsSortBzShowRvViewHolder(@NonNull View itemView) {
            super(itemView);
            type = itemView.findViewById(R.id.view_goods_Bz_TypeName);
            no = itemView.findViewById(R.id.view_goods_Bz_No);
            price = itemView.findViewById(R.id.view_goods_Bz_Price);
            title = itemView.findViewById(R.id.view_goods_Bz_Desc);
            fabudata = itemView.findViewById(R.id.view_goods_Bz_FaBuData);
            shishidata = itemView.findViewById(R.id.view_goods_Bz_ShiShiData);
            car = itemView.findViewById(R.id.view_goods_Bz_Car);
//            item = itemView.findViewById(R.id.view_goods_Bz_itemview);
        }
    }

    public interface GoodsBzRvListener {
        void addBzCar(String skuStr,GoodsSortBzShowRvViewHolder holder);
//        void addBzSxCar(String skuStr);
    }
}
